﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using JavaSubstitutes;

namespace Sprites
{
	public class BulletBill: Sprite
	{
		public int width = 4;
		public int height = 24;

		private LevelScene world;
		public int facing;

		public bool avoidCliffs = false;
		public int anim;

		public bool dead = false;
		private int deadTime = 0;


		public BulletBill(LevelScene world, double x, double y, int dir)
		{
			sheet = Art.enemies;

			this.x = x;
			this.y = y;
			this.world = world;
			xPicO = 8;
			yPicO = 31;

			height = 12;
			facing = 0;
			wPic = 16;
			yPic = 5;

			xPic = 0;
			yDelta = -5;
			this.facing = dir;
		}

		public override void collideCheck()
		{
			if (dead) return;

			double xMarioD = world.mario.x - x;
			double yMarioD = world.mario.y - y;
			//double w = 16;
			if (xMarioD > -16 && xMarioD < 16)
			{
				if (yMarioD > -height && yMarioD < world.mario.height)
				{
					if (world.mario.yDelta > 0 && yMarioD <= 0 && (!world.mario.onGround || !world.mario.wasOnGround))
					{
						world.mario.stomp(this);
						dead = true;

						xDelta = 0;
						yDelta = 1;
						deadTime = 100;
					}
					else
					{
						world.mario.getHurt();
					}
				}
			}
		}

		public override void move()
		{
			if (deadTime > 0)
			{
				deadTime--;

				if (deadTime == 0)
				{
					deadTime = 1;
					for (int i = 0; i < 8; i++)
					{
						world.addSprite(new Sparkle((int)(x + RandomExtensions.random() * 16 - 8) + 4, (int)(y - RandomExtensions.random() * 8) + 4, (double)(RandomExtensions.random() * 2 - 1), (double)RandomExtensions.random() * -1, 0, 1, 5));
					}
					spriteContext.removeSprite(this);
				}

				x += xDelta;
				y += yDelta;
				yDelta *= 0.95f;
				yDelta += 1;

				return;
			}

			double sideWaysSpeed = 4f;

			xDelta = facing * sideWaysSpeed;
			xFlipPic = facing == -1;
			move(xDelta, 0);
		}

		private bool move(double xa, double ya)
		{
			x += xa;
			return true;
		}

		public override bool fireballCollideCheck(Fireball fireball)
		{
			if (deadTime != 0) return false;

			double xD = fireball.x - x;
			double yD = fireball.y - y;

			if (xD > -16 && xD < 16)
			{
				if (yD > -height && yD < fireball.height)
				{
					return true;
				}
			}
			return false;
		}

		public override bool shellCollideCheck(Shell shell)
		{
			if (deadTime != 0) return false;

			double xD = shell.x - x;
			double yD = shell.y - y;

			if (xD > -16 && xD < 16)
			{
				if (yD > -height && yD < shell.height)
				{
					world.sound.play(Art.samples[Art.SAMPLE_MARIO_KICK], this, 1, 1, 1);

					dead = true;

					xDelta = 0;
					yDelta = 1;
					deadTime = 100;

					return true;
				}
			}
			return false;
		}
	}
}